<template>
  <div>
    <!-- table -->
    <!-- 现场人员信息记录 - 再次 -->
    <BreakSpace label="基本信息" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel ref="formPanel1" :list="formList" :cols="3" :formType="'add'" labelWidth="140" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div style="margin-bottom: 1rem;"></div>
    <BreakSpace label="故障信息" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="table1"
      :columns="columns"
      :params="fetchParams"
      :isPagination = false
      :dataSource="dataList"
      :datakey="'items'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      selectionType="single"
      height="auto"
      :isSelectColumn=false
    >
    </FilterTable>
    <div style="margin-bottom: 1rem;"></div>
    <FormPanel ref="formPanel5" :list="formList5" :cols="3" :formType="'add'" labelWidth="140" :isSubmitBtn="false"/>
    <div style="margin-bottom: 2rem;"></div>
    <BreakSpace label="经销商-车厂沟通历史" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="table2"
      :columns="columns2"
      :params="fetchParams"
      :isPagination = false
      :dataSource="dataList2"
      :datakey="'items'"
      :onColumnsChange="columns => (this.columns2 = columns2)"
      :onRowSelectChange="selectChange"
      selectionType="single"
      height="auto"
      :isSelectColumn=false
    ></FilterTable>
    <div style="margin-bottom: 2rem;"></div>
    <BreakSpace label="解决方案" :containerStyle="{ marginBottom: '10px' }" />
    <FormPanel ref="formPanel4" :list="formList4" :cols="3" :formType="'add'" labelWidth="140" :isSubmitBtn="false" @formChange="formChangeHandle4" />
    <div style="margin-bottom: 1rem;"></div>
    <div style="padding-top: 3rem;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
    <BaseDialog :title="'查看附件'" :visible.sync="upVisible" destroyOnClose>
      <uploadFile :rowData="oemId" @close="upVisible = false"></uploadFile>
    </BaseDialog>
  </div>
</template>
<script>
import uploadFile from '@/pages/claimDemage/TechnicalSupport/uploadFile.vue';

//字典
import { getPartType } from '@/api/dictionary/index';
import { selectById, editSave }  from '@/api/technicalReportManagement/technicalSupportManagement_OEM/onSitePersonnelRecords/index';
import moment from "moment";

export default {
  props: ['rowData'],
  // mixins: [dictionary],
  components: {
    uploadFile
  },
  data() {
    return {
      dataList: [],
      dataList2: [],
      dataList3: [],
      dataList4: [],
      upVisible: false,
      visible: false,
      columns: this.creatHeader(),
      columns2: this.creatHeader2(),
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList: this.createFormList(),
      formList4: this.createFormList4(),
      formList5: this.createFormList5(),
      saveData: null,
      saveData2: null,
      saveData3: null,
      saveData4: null,
      modelId: null,
      siteIds: [],
      modelCode: null,
      fileIds: [],
      fileIdss: [],
      saveFlag: 55521004,
      oemId: null
    }
  },
  created() {
    this.chuqi();
    this.init();
  },
  methods: {
    async init() {
      const newVar = await selectById({id: this.rowData.techId});
      this.editData = newVar.data;
      this.modelId = newVar.data.modelId;
      this.modelCode = newVar.data.modelCode;
      console.log("=== 通过ID查询结果返回 ==== ",this.editData," ======");
      this.formList.forEach(x => {
        console.log(newVar);
        x.initialValue = newVar.data[x.fieldName];
      });
      this.formList4.forEach(x => {
        x.initialValue = newVar.data[x.fieldName];
      });
      this.formList5.forEach(x => {
        if (x.fieldName == 'applyFileList') {
          x.initialValue = newVar.data.applyFileList.map((f) => ({ name: f.title, url: f.fileUrl }));
        }
      });
      this.$refs.table1.$refs.pageTable.EXECUTE_INSERT(newVar.data.faultList);
      this.$refs.table2.$refs.pageTable.EXECUTE_INSERT(newVar.data.oemList);

      //故障信息
      newVar.data.siteList.map(x => {
        this.siteIds.push(x.siteId);
      });

      let ids = '';
      this.siteIds.map(x => {
        ids += x + ','
      });
      this.siteIds = ids;

      //附件1
      newVar.data.dealFileList.map(x =>{
        this.fileIds.push(x.fileId);
      });
      let idss = '';
      this.fileIds.map(x => {
        idss += x + ','
      });
      this.fileIds = idss;

      //附件2
      newVar.data.resultFileList.map(x =>{
        this.fileIdss.push(x.fileId);
      });
      let idsss = '';
      this.fileIdss.map(x => {
        idsss += x + ','
      });
      this.fileIdss = idsss;


      console.log(this.siteIds,"siteIds")
      console.log(this.fileIds,"fileIds")
      console.log(this.fileIdss,"fileIdss")
      console.log("两个附件ID加一起",this.fileIds + this.fileIdss)
    },
    async chuqi() {
      //是否首次咨询
      const res1 = await getPartType({type: 5500});
      if (res1.resultCode == 200) {
        this.formList.find(x => x.fieldName == 'ifFirst').itemList = res1.data.map(y => ({
          text: y.codeCnDesc,
          value: y.codeId
        }));
      }
      //状态
      const res2 = await getPartType({type: 5552});
      if (res2.resultCode == 200) {
        this.formList.find(x => x.fieldName == 'techStatus').itemList = res2.data.map(y => ({
          text: y.codeCnDesc,
          value: y.codeId
        }));
      }
      //受理类型
      const res3 = await getPartType({type: 5553});
      if (res3.resultCode == 200) {
        this.formList.find(x => x.fieldName == 'acceptType').itemList = res3.data.map(y => ({
          text: y.codeCnDesc,
          value: y.codeId
        }));
      }
      //评价结果
      const res6 = await getPartType({type: 5554});
      if (res6.resultCode == 200) {
        this.formList4.find(x => x.fieldName == 'dealType').itemList = res6.data.map(y => ({
          text: y.codeCnDesc,
          value: y.codeId
        }));
      }
    },
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    formChangeHandle2(val) {
      console.log(val);
      this.saveData2 = val;
    },
    formChangeHandle3(val) {
      console.log(val);
      this.saveData3 = val;
    },
    formChangeHandle4(val) {
      console.log(val);
      this.saveData4 = val;
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //保存
    async saveDialog() {

      this.$refs.formPanel1.SUBMIT_FORM();
      this.$refs.formPanel2.SUBMIT_FORM();
      this.$refs.formPanel3.SUBMIT_FORM();
      this.$refs.formPanel4.SUBMIT_FORM();

      console.log('newSave', this.saveData);
      console.log('newSave2', this.saveData2);
      console.log('newSave3', this.saveData3);
      console.log('newSave4', this.saveData4);

      let save = {};
      save = Object.assign({}, this.saveData, this.saveData2, this.saveData3, this.saveData4);


      this.dataList4.map(x => {
        delete  x.siteId;
      });


      let dealFileList = [];

      this.saveData2.dealFileList.map((x) => {
        dealFileList.push({ title: x.name, fileUrl: x.url});
      });


      let resultFileList = [];

      this.saveData3.resultFileList.map((x) => {
        resultFileList.push({ title: x.name, fileUrl: x.url});
      });

      save.techStatus = this.saveFlag;

      save.dealFileList = dealFileList;
      save.resultFileList = resultFileList;

      save.siteList = this.dataList4;

      save.disDate = moment(this.saveData3.disDate).format('YYYY-MM-DD HH:mm:ss');
      save.estDate = moment(this.saveData3.estDate).format('YYYY-MM-DD HH:mm:ss');
      save.actDate = moment(this.saveData3.actDate).format('YYYY-MM-DD HH:mm:ss');
      save.evaluateAt = moment(this.saveData4.evaluateAt).format('YYYY-MM-DD HH:mm:ss');

      //techId主表主键
      save.techId = this.editData.techId;
      //系统ID
      save.appId = this.editData.appId;
      //所有者代码
      save.ownerCode = this.editData.ownerCode;
      //所有者的父组织代码
      save.ownerParCode = this.editData.ownerParCode;
      //组织ID
      save.orgId = this.editData.orgId;
      //是否删除
      save.isDeleted = this.editData.isDeleted;
      //创建人ID
      save.createdBy = this.editData.createdBy;
      //创建时间
      save.createdAt = this.editData.createdAt;
      //版本号
      save.recordVersion = this.editData.recordVersion;
      //经销商ID
      save.dealerId = this.editData.dealerId;
      //车型ID
      save.modelId = this.modelId;
      //车型代码
      save.modelCode = this.modelCode;
      //首次提交时间
      save.firstDate = this.editData.firstDate;
      //处理方式
      save.dealType = this.editData.dealType;
      //销售日期
      save.saleDate = this.editData.saleDate;
      //生产日期
      save.prodDate = this.editData.prodDate;
      //维修方案
      save.cmsClaimCase = this.editData.cmsClaimCase;
      //故障现象描述
      save.faultInfo = this.editData.faultInfo;
      //检修经过
      save.cmsClaimProcess = this.editData.cmsClaimProcess;
      //维修结果
      save.cmsClaimResult = this.editData.cmsClaimResult;
      //接单人
      save.takerName = this.editData.takerName;
      //申请日期
      save.applyDate = this.editData.applyDate;
      //联系电话
      save.sitePhone = this.editData.sitePhone;
      //邮箱
      save.siteEmail = this.editData.siteEmail;
      //评价结果
      save.evaluateResult = this.editData.evaluateResult;
      //评价人
      save.evaluateBy = this.editData.evaluateBy;
      //评价时间
      save.evaluateAt = this.editData.evaluateAt;
      //评价描述
      save.evaluateRemarks = this.editData.evaluateRemarks;

      save.siteIds = this.siteIds;
      console.log("两个附件ID加一起",this.fileIds + this.fileIdss)
      save.fileIds = this.fileIds + this.fileIdss;

      console.log(save);

      let res = await editSave(save);
      console.log(res);
      if (res.resultCode === 200) {
        this.$emit('save');
        return this.$notify.success({title: '提示信息', message: '保存成功'});
      } else {
        this.$notify.error({title: '提示信息', message: res.errMsg || '保存失败!'});
        this.closeDialog();
      }

    },
    //提交
    submit() {
      this.saveFlag = 55521005;
      if (this.dataList4.length > 0) {
        this.saveDialog();
      } else {
        return this.$notify.error({title: '提示信息', message: '请添加现场人员处理过程后再进行提交！'});
      }
    },
    //保存
    save() {
      this.saveFlag = 55521004;
      this.saveDialog();
    },
    //取消操作
    closeDialog() {
      this.$emit('close', {visible: false, data: this.selectedRows, type: 'close'});
    },
    AddRocessTurnOn() {
      this.visible = true;
    },
    closeAddRocess() {
      this.visible = false;
    },
    deleteHandle(val) {
      this.$refs.table4.$refs.pageTable.EXECUTE_DELETE([val]);
    },
    saveAddRocess(obj) {
      console.log('aaaaaaa',obj)
      this.$refs.table4.$refs.pageTable.EXECUTE_INSERT(obj);
      this.visible = false;
    },
    fileOemTurnOn(val) {
      this.oemId = val.oemId;
      this.upVisible = true;
    },
    fileQaTurnOn(val) {
      this.oemId = val.qaId;
      this.upVisible = true;
    },
    //基本信息
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '技术报告单号',
          fieldName: 'techNo',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'techStatus',
          itemList: [],
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'SELECT',
          label: '是否首次咨询',
          fieldName: 'ifFirst',
          itemList: [],
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择经销商代码',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '所属区域',
          fieldName: 'belongArea',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '经销商联系人',
          fieldName: 'dcPerson',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '经销商联系人电话',
          fieldName: 'dcTel',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'DATE',
          label: '提交时间',
          fieldName: 'firstDate',
          valueFormat: 'yyyy-MM-dd',
          dateType: 'datetime',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请选择VIN',
          readonly: true,// 只读
          disabled: true// 置会
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelName',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '行驶里程',
          fieldName: 'driveMile',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'DATE',
          label: '生产日期',
          fieldName: 'prodDate',
          valueFormat: 'yyyy-MM-dd',
          dateType: 'datetime',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'DATE',
          label: '销售日期',
          fieldName: 'saleDate',
          valueFormat: 'yyyy-MM-dd',
          dateType: 'datetime',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'licenseNo',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'SELECT',
          label: '故障类型',
          fieldName: 'acceptType',
          placeholder: '受理类型',
          itemList: [],
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '说明',
          fieldName: 'techInfos',
          readonly: true,// 只读
          disabled: true // 置会
        },
      ];
    },
    //故障信息
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {title: '一级', dataIndex: 'sysName1', sorter: true},
        {title: '二级', dataIndex: 'sysName2', sorter: true},
        {title: '三级', dataIndex: 'sysName3', sorter: true},
        {title: '故障名称', dataIndex: 'sysName4', sorter: true},
        {title: '故障现象代码', dataIndex: 'faultCode', sorter: true},
        {title: '故障现象名称', dataIndex: 'faultName', sorter: true},
        {title: '故障Id', dataIndex: 'faId', sorter: true, hidden: true},
        {title: '故障码', dataIndex: 'errorCode', sorter: true},
        {title: '故障码触发条件', dataIndex: 'faultCondition', sorter: true},
        {title: '本车是否首发', dataIndex: 'ifFirst', sorter: true, hidden: true},
        {title: '本车是否首发', dataIndex: 'ifFirstStr', sorter: true},
        {title: '备注', dataIndex: 'remarks', sorter: true}
      ];
    },
    //故障信息附件
    createFormList5() {
      return [
        {
          type: 'UPLOAD_FILE',
          label: '上传文件',
          fieldName: 'applyFileList',
          placeholder: '上传文件...',
          initialValue: [],
          readonly: true,// 只读
          disabled: true, // 置会
          upload: {
            actionUrl: '/api/file/oss/upload',
            limit: 2,
            // tipText: '只能上传pdf格式'
          }
        }
      ];
    },
    //经销商-车厂沟通历史
    creatHeader2() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 80,
          render: (props, h) => {
            return (
              <div>
                <multiuse-button  size="mini" type="text" onClick={() => this.fileOemTurnOn(props.row)}>
                  查看附件
                </multiuse-button>
              </div>
            );
          }
        },
        { title: '经销商人员', dataIndex: 'dealerUser', sorter: true },
        { title: '经销商提交时间', dataIndex: 'dApplyDate', sorter: true , dateFormat: 'yyyy-MM-dd'},
        { title: '故障现象描述', dataIndex: 'faultInfo', sorter: true },
        { title: '检修经过', dataIndex: 'cmsClaimProcess', sorter: true },
        { title: '维修结果', dataIndex: 'cmsClaimResult', sorter: true },
        { title: '车厂处理人', dataIndex: 'dealBy', sorter: true },
        { title: '车厂处理时间', dataIndex: 'dealDate', sorter: true },
        { title: '维修方案', dataIndex: 'cmsClaimCase', sorter: true },
        // { title: '附件', dataIndex: 'date1', sorter: true },
      ];
    },
    //解决方案
    createFormList4() {
      return [
        {
          type: 'SELECT',
          label: '处理方式',
          fieldName: 'dealType',
          placeholder: '请选择',
          disabled: true, // 置会
          itemList: []
        },
        {
          type: 'TEXT_AREA',
          label: '质量处理意见',
          fieldName: 'qualityTreatmentOpinions',
          selfCols: 3,// 元素占据几行
          disabled: true, // 置会
          rows: 2
        },
        {
          type: 'UPLOAD_FILE',
          label: '上传文件',
          fieldName: 'applyFileListss',
          placeholder: '上传文件...',
          initialValue: [],
          readonly: true,// 只读
          disabled: true, // 置会
          upload: {
            actionUrl: '/api/file/oss/upload',
            limit: 2,
            // tipText: '只能上传pdf格式'
          }
        }
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
